package com.example.demo.controller;

import com.example.demo.annotation.UserLoginToken;
import com.example.demo.service.MenuService;
import com.example.demo.utils.Result;
import com.example.demo.vo.MenuVo;
import com.example.demo.vo.userVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class menuComtraoller {

    @Autowired
    MenuService menuService;

    @ResponseBody
    @RequestMapping("/getMenu")
    public Result getMenu(){
        List<MenuVo> mList = menuService.getMenu();
        return Result.success(mList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/add")
    public Result setMenu(@RequestBody MenuVo menuVo){
        int type = menuService.setMenu(menuVo);
        if(type == 1){
            return Result.success();
        }else {
            return Result.failure(-1,"添加失败");
        }
    }

    @ResponseBody
    @UserLoginToken
    @RequestMapping("list")
    public Result getList(
                          int currentPage,
                          int pageSize){
        return menuService.getlist(currentPage,pageSize);
    }

    /**
     * 获取所有目录
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllcatalogue")
    public Result getAllcatalogue(){
        List<MenuVo> mList = menuService.getAllcatalogue();
        return Result.success(mList);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/del")
    public Result del(@RequestParam Integer id){
        return menuService.del(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/edit")
    public Result edit(@RequestBody MenuVo menuVo){
        return menuService.update(menuVo);
    }
}
